<?php
/**
 * Created by PhpStorm.
 * User: Inhere
 * Date: 14-4-13
 * Time: 上午9:27
 */
namespace ulue\base;

use Ioc;
use ulue\libs\helpers\ArrayHelper;

abstract class Base
{

    /**
     * current Application's instance
     * @var \ulue\web\Application
     */
    static public $app      = null;

    /**
     * current Contianer's instance
     * @var Contianer
     */
    static public $di      = null;

    /**
     * 项目路径标记 用于替换真实 PROJECT_PATH 路径
     */
    const PATH_MARK = '&lt;ROOT&gt';

    private function __construct()
    {}

    static public function version()
    {
        return '1.2.1';
    }

    static public function isProdEnv()
    {
        return APP_ENV == 'prod';
    }

    static public function loadAttrs($object, array $options)
    {
        foreach ($options as $property => $value) {
            $object->$property = $value;
        }
    }

    /**
     * @param $namespace
     * @param $path
     */
    static public function setNamespace($namespace, $path)
    {
        Ioc::$box['loader']->setNs($namespace, $path);
    }

    /**
     * @param $namespace
     * @return bool|string
     */
    static public function resolveNamespace($namespace)
    {
        return Ioc::$box['loader']->resolveNs($namespace);
    }

    /**
     * 替换真实 PROJECT_PATH 路径
     */
    static public function getSafePath($path, $mark=self::PATH_MARK)
    {
        return str_replace(PROJECT_PATH, $mark, str_replace('\\', '/', $path) );
    }

//////////////////////////////////////// path/alias Manager ////////////////////////////////////////

    /**
     * 获取项目的路径,结尾无路径分隔符 '/'
     * @param $key
     * @return mixed|null|string
     */
    static public function getPath($key)
    {
        return \Ioc::get('pathMgr')->getPath($key);
    }

    /**
     * 获取项目的目录名称
     * @param $key
     * @return null|string
     */
    static public function getDir($key)
    {
        return \Ioc::get('pathMgr')->getDir($key);
    }

    /**
     * 设置别名的路径
     * @param $alias
     * @param $path
     * @throws \DRuntimeException
     * @return $this
     */
    static public function setAlias($alias, $path)
    {
        return \Ioc::get('pathMgr')->alias($alias,$path);
    }

    /**
     * 解析别名得到路径
     * @param $alias
     * @return mixed|string
     */
    static public function resolveAlias($alias)
    {
        return \Ioc::get('pathMgr')->resolveAlias($alias);
    }

//////////////////////////////////////// other ////////////////////////////////////////

    /**
     * 语言包翻译
     * @param  string $value
     * @param  array $repeat
     * @return string
     */
    static public function tl($value, $repeat)
    {
        if ( !Ioc::exists('app.lang') ) {
            throw new \DRuntimeException('Language service has not been registered');
        }

        return Ioc::get('app.lang')->translation($value, $repeat);
    }

    /**
     * @param $class
     * @return bool|string
     */
    static public function customAutoload($class)
    {
        if ( substr($class, -9) === 'Exception')
        {
            return SYS_PATH . '/libs/debug/driver/' . $class .'.php';
        }

        return false;
    }

    /**
     * @param int $msg
     */
    static public function quit($msg=0)
    {
        if (self::$app!=null)
        {
            self::$app->quit();
        }

        exit($msg);
    }

    /**
     * 正式环境名：[prod]
     * 非正式环境应用配置 在 config/下对应 APP_ENV 值的目录
     * e.g. APP_ENV=='dev' --> file: COMM_PATH."/config/dev/config.php"
     * @throws DRuntimeException
     */
    static public function loadConfiguration($env)
    {
        $config = require COMM_PATH . '/config/config.php';

        if ( $env != 'prod' ) {

            $otherEnv = COMM_PATH . '/config/' . $env . '/config.php';

            if ( !file_exists($otherEnv) ) {
                throw new \DRuntimeException("设置的应用环境[".$env."]；没有对应的配置文件。<br> 请创建：$confFile");
            }

            $config = ArrayHelper::merge( $config, require $otherEnv);
        }

        return $config;
    }

    /**
     * 载入服务配置
     * 正式环境名：[prod]
     * 非正式环境应用配置 在 config/下对应 APP_ENV 值的目录
     * e.g. APP_ENV=='dev' --> file: COMM_PATH."/config/dev/service.php"
     */
    static public function loadServiceConfig($env)
    {
        $service = require COMM_PATH . '/config/service.php';

        if ( $env != 'prod' ) {

            $otherEnv = COMM_PATH . '/config/' . $env . '/service.php';

            if ( file_exists($otherEnv) ) {
                $service = ArrayHelper::merge( $service, require $otherEnv);
            }
        }

        return $service;
    }

    /**
     * @return array|mixed
     */
    static public function readRouteConfiguration()
    {
        /**
         * 正式环境名：[prod]
         * 非正式环境路由配置 在 config/下对应 APP_ENV 值的目录
         * e.g. APP_ENV=='dev' --> file: COMM_PATH."/config/dev/route.php"
         */
        $dirName  = APP_ENV == 'prod' ? '' : '/'.APP_ENV;
        $confFile = COMM_PATH."/config{$dirName}/route.php";

        // $configArr = ArrayHelper::merge(
        //     require $configFiles['app'],
        //     require $confFile
        // );

        // $confFile = COMM_PATH.'/config/route.php';

        if ( file_exists($confFile))
        {
            return require($confFile);
          //return \Symfony\Component\Yaml\Yaml::parse($confFile);
        }

        return [];
    }

}//end class Base



